package com.hcy.performancepro.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fuled.test.tool_entity.RecordControllerPerformanceTool;
import com.fuled.test.tool_entity.RecordDaoPerformanceTool;
import com.fuled.test.tool_entity.RecordEntityPerformanceTool;
import com.fuled.test.tool_entity.RecordServiceImplPerformanceTool;
import com.hcy.performancepro.dao.performanceTool.PerformanceToolControllerDao;
import com.hcy.performancepro.dao.performanceTool.PerformanceToolEntityDao;
import com.hcy.performancepro.po.performanceTool.RecordController;
import com.hcy.performancepro.po.performanceTool.RecordDao;
import com.hcy.performancepro.po.performanceTool.RecordEntity;
import com.hcy.performancepro.po.performanceTool.RecordService;
import com.hcy.performancepro.service.performanceTool.PerformanceToolDaoService;
import com.hcy.performancepro.service.performanceTool.PerformanceToolServiceService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
@RequiredArgsConstructor
public class MyMessageListener implements MessageListener {
    private final PerformanceToolControllerDao performanceToolControllerDao;

    private final PerformanceToolServiceService performanceToolServiceService;

    private final PerformanceToolDaoService performanceToolDaoService;

    private final PerformanceToolEntityDao performanceToolEntityDao;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String messageStr = new String(message.getBody(), StandardCharsets.UTF_8);
        CompletableFuture.runAsync(()->{
            // 创建ObjectMapper实例
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.registerModule(new JavaTimeModule());
            try {
                RecordEntityPerformanceTool recordEntityPerformanceTool = objectMapper.readValue(messageStr, RecordEntityPerformanceTool.class);
                //if(recordEntityPerformanceTool==null) return;
                RecordEntity recordEntity = new RecordEntity();
                BeanUtils.copyProperties(recordEntityPerformanceTool, recordEntity);
                Long costTime = recordEntityPerformanceTool.getTotalCostTime();
                recordEntity.setTotalCostTime(costTime);
                performanceToolEntityDao.insert(recordEntity);
                RecordController recordController = new RecordController();
                List<RecordService> recordServiceList = new ArrayList<>();
                List<RecordDao> recordDaoList = new ArrayList<>();
                RecordControllerPerformanceTool controllerTool = recordEntityPerformanceTool.getRecordControllerPerformanceTool();
                if (controllerTool != null) {
                    BeanUtils.copyProperties(controllerTool, recordController);
                    if (controllerTool.getParams().isEmpty())
                        recordController.setParams("");
                    else
                        recordController.setParams(objectMapper.writeValueAsString(controllerTool.getParams()));
                    if (controllerTool.getReturnMessage() == null)
                        recordController.setReturnMessage("");
                    else
                        recordController.setReturnMessage(objectMapper.writeValueAsString(controllerTool.getReturnMessage()));
                    performanceToolControllerDao.insert(recordController);
                }
                ArrayList<RecordServiceImplPerformanceTool> serviceTool = recordEntityPerformanceTool.getRecordServiceImplPerformanceTools();
                if (serviceTool != null) {
                    for (RecordServiceImplPerformanceTool recordServiceImplPerformanceTool : serviceTool) {
                        RecordService recordService = new RecordService();
                        BeanUtils.copyProperties(recordServiceImplPerformanceTool, recordService);
                        if (recordServiceImplPerformanceTool.getParams().isEmpty())
                            recordService.setParams("");
                        else
                            recordService.setParams(objectMapper.writeValueAsString(recordServiceImplPerformanceTool.getParams()));
                        if (recordServiceImplPerformanceTool.getReturnMessage() == null)
                            recordService.setReturnMessage("");
                        else
                            recordService.setReturnMessage(objectMapper.writeValueAsString(recordServiceImplPerformanceTool.getReturnMessage()));
                        recordServiceList.add(recordService);
                    }
                    performanceToolServiceService.saveBatch(recordServiceList);
                }
                ArrayList<RecordDaoPerformanceTool> daoTool = recordEntityPerformanceTool.getRecordDaoPerformanceTool();
                if (daoTool != null) {
                    for (RecordDaoPerformanceTool recordDaoPerformanceTool : daoTool) {
                        RecordDao recordDao = new RecordDao();
                        BeanUtils.copyProperties(recordDaoPerformanceTool, recordDao);
                        if (recordDaoPerformanceTool.getParams().isEmpty())
                            recordDao.setParams("");
                        else
                            recordDao.setParams(objectMapper.writeValueAsString(recordDaoPerformanceTool.getParams()));
                        if (recordDaoPerformanceTool.getReturnMessage() == null)
                            recordDao.setReturnMessage("");
                        else
                            recordDao.setReturnMessage(objectMapper.writeValueAsString(recordDaoPerformanceTool.getReturnMessage()));
                        recordDaoList.add(recordDao);
                    }
                    performanceToolDaoService.saveBatch(recordDaoList);
                }
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });
    }
}

